﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="RulesListDialog.cs" company="YellowFish">
//   Copyright © YellowFish 2009
// </copyright>
// <summary>
//   Defines the RulesListDialog type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MobilePal.Dialogs
{
  #region Imported Namespaces


  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Windows.Forms;

  using Rules;

  using State;


  #endregion


  /// <summary>
  /// Describes and manipulates the rules list dialog.
  /// </summary>
  public partial class RulesListDialog: ListDialogBase
  {
    #region Constants and Fields


    private readonly ActivityCollection _activityCollection;
    private readonly ActivityCategoryCollection _categoryCollection;
    private readonly ActivityRuleCollection _ruleCollection;


    #endregion


    #region Constructors and Destructors


    /// <summary>
    /// Initializes a new instance of the <see cref="RulesListDialog"/> class.
    /// </summary>
    /// <param name="ruleCollection">
    /// The rule collection.
    /// </param>
    /// <param name="activityCollection">
    /// The activity collection.
    /// </param>
    /// <param name="categories">
    /// The categories.
    /// </param>
    public RulesListDialog(ActivityRuleCollection ruleCollection, ActivityCollection activityCollection, 
                           ActivityCategoryCollection categories)
    {
      _ruleCollection = ruleCollection;
      _activityCollection = activityCollection;
      _categoryCollection = categories;

      InitializeComponent();

      ListView = rulesList;
      UpdateCloseMenuItem();
    }


    /// <summary>
    /// Gets the list view item.
    /// </summary>
    /// <param name="rule">
    /// The rule.
    /// </param>
    /// <returns>
    /// </returns>
    protected override string GetListViewItemText(object rule)
    {
      return ((ActivityRule)rule).Name;
    }


    /// <summary>
    /// Gets the sorted items list.
    /// </summary>
    /// <returns>
    /// </returns>
    protected override List<object> GetSortedItemsList()
    {
      var list = new List<ActivityRule>(_ruleCollection);
      list.Sort((rule1, rule2) => string.Compare(rule1.Name, rule2.Name));

      return new List<object>(list.Cast<object>());
    }


    /// <summary>
    /// Called when show check boxes flag changed.
    /// </summary>
    protected override void OnShowCheckBoxesChanged()
    {
      UpdateCloseMenuItem();
    }


    private void UpdateCloseMenuItem()
    {
      closeMenuItem.Text = ShowCheckBoxes ? "Cancel" : "Close";
    }


    /// <summary>
    /// Updates the controls.
    /// </summary>
    protected override void UpdateControls()
    {
      editMenuItem.Enabled = FocusedListViewItem != null;
      removeMenuItem.Enabled = FocusedListViewItem != null;
    }


    private void AddMenuItemClick(object sender, EventArgs e)
    {
      var dialog = new EditRuleDialog(null, _activityCollection, _categoryCollection);
      dialog.Text = "Add Rule";

      ShowEditRuleDialog(dialog);

      _ruleCollection.Add(dialog.Rule);

      BuildListView();
    }


    private void CloseMenuItemClick(object sender, EventArgs e)
    {
      DialogResult = ShowCheckBoxes ? DialogResult.Cancel : DialogResult.OK;

      Close();
    }


    private void RemoveMenuItemClick(object sender, EventArgs e)
    {
      var rule = (ActivityRule)FocusedItem;

      DialogResult result =
        MessageBox.Show(string.Format("Are you sure you want to remove rule '{0}'?", rule.Name),
                        "Remove Rule?", MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
                        MessageBoxDefaultButton.Button1);

      if (result != DialogResult.OK)
      {
        return;
      }

      _ruleCollection.Remove(rule);

      RemoveItem(FocusedListViewItem);
    }


    private void EditMenuItemClick(object sender, EventArgs e)
    {
      var rule = (ActivityRule)FocusedItem;
      var dialog = new EditRuleDialog(rule, _activityCollection, _categoryCollection);
      dialog.Text = "Edit Rule";

      DialogResult result = dialog.ShowDialog();

      if (result == DialogResult.OK)
      {
        BuildListView();
      }
    }


    private void ShowEditRuleDialog(EditRuleDialog dialog)
    {
      DialogResult result = dialog.ShowDialog();

      if (result != DialogResult.OK)
      {
        return;
      }

      ActivityRule rule = dialog.Rule;

      var rules = new List<ActivityRule>(_ruleCollection.Where(activityRule => activityRule.Name == rule.Name));

      if (rules.Count <= 1)
      {
        return;
      }

      MessageBox.Show(string.Format("A rule with name '{0}' already exists.", rule.Name),
                      "Invalid Rule Name", MessageBoxButtons.OK, MessageBoxIcon.Hand,
                      MessageBoxDefaultButton.Button1);

      ShowEditRuleDialog(dialog);
    }


    #endregion

  }
}